/*
 * Appointment.java
 */
package edu.gatech.oad.schedule;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import edu.gatech.oad.persistence.PersistentObject;
import edu.gatech.oad.records.TreatmentRecord;
import edu.gatech.oad.user.Doctor;
import edu.gatech.oad.user.Patient;

/**
 * Represents a patient's appointment.
 * @author Joey Slater
 * @version 1.00
 */
public class Appointment extends PersistentObject{
	/**
	 * serialized number 
	 */
	private static final long serialVersionUID = 1L; 
	
	/**
	 * date for the appointment
	 */
	private Date date;  
	
	/**
	 * Reasons why the user needed an appoinment
	 */
	private List<String> reason = new ArrayList<String>();
	
	/**
	 * Treatment record for the appointment that will be connected to the appointment
	 */
	private TreatmentRecord treatmentRecord;
	
	/**
	 * Are the patients here or have completed their appointment
	 */
	private boolean checkedIn, treated;
	
	/**
	 * The patient's doctor
	 */
	private Doctor doctor;
	
	/**
	 * Who the appointment belongs to
	 */
	private Patient patient;
	
	/**
	 * Constructor
	 * @param newDate appointment's dates
	 * @param newReason patient's reason for the appointment
	 */
	public Appointment(Date newDate, List<String> newReason){
		this.date = newDate; 
		this.reason = newReason;
		this.checkedIn = false;
		this.treated = false;
	}
	
	//Getters and Setters
	/**
	 * @param updatedDate edit the appointment's date
	 */
	public void editDate(Date updatedDate){
		date = updatedDate;
	}
	
	/**
	 * @return the date of the appointment
	 */
	public Date getDate(){
		return date;
	}
	
	/**
	 * @param newReason edit the appointment's reason for visit
	 */
	public void editReason(List<String> newReason){
		reason = newReason;
	}
	
	/**
	 * @return the appointment's reason 
	 */
	public List<String> getReason(){
		return reason;
	}
	
	/**
	 * Cause the patient's appointment to be classified as the opposite of what was.
	 */
	public void changeCheckIn(){
		checkedIn = !checkedIn;
	}
	
	/**
	 * @return whether the patient is checked in or not
	 */
	public boolean isCheckedIn(){
		return checkedIn;
	}
	
	/**
	 * Changes the patient's appointment to have the patient be classified 
	 * of what the patient was.
	 */
	public void changeTreated(){
		treated = !treated;
	}
	
	/**
	 * @return if the patient has been treated
	 */
	public boolean isTreated(){
		return treated;
	}
	
	/**
	 * @return the treatment record associated with the appointment
	 */
	public TreatmentRecord getTreatmentRecord(){
		return treatmentRecord; //Used for linking Treatment Records
	}
	
	/**
	 * @param treatmentRecord setting the linking appointment
	 */
	public void setTreatmentRecord(TreatmentRecord treatmentRecord){
		this.treatmentRecord = treatmentRecord;
	}

	/**
	 * Sets the doctor with whom this appointment is scheduled.
	 * @param doctor the doctor to set
	 */
	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}

	/**
	 * Gets the doctor with whom this appointment is scheduled.
	 * @return the doctor
	 */
	public Doctor getDoctor() {
		return doctor;
	}

	/**
	 * Sets the patient whose appointment this is.
	 * @param patient the patient to set
	 */
	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	/**
	 * Gets the patient whose appointment this is.
	 * @return the patient
	 */
	public Patient getPatient() {
		return patient;
	}
	
	public boolean withinThirtyMinutes(){
		Calendar today = Calendar.getInstance();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if(today.get(Calendar.DATE) == cal.get(Calendar.DATE) && 
				today.get(Calendar.MONTH) == cal.get(Calendar.MONTH)&& 
				today.get(Calendar.YEAR) == cal.get(Calendar.YEAR)){
				if(cal.before(today)){
					return true;	
				}
		}
		return false;
	}
	
	public Appointment nextAppointment(){
		if(this.withinThirtyMinutes()){
			return this;
		}
		return null;
	}
	
	/**
	 * Overriding the generic toString method, the appointment will 
	 * have the date and the doctor and reason  
	 * @return the appointmet's description 
	 */
	public String toString(){
		return (DateFormat.getDateInstance(DateFormat.SHORT).format(this.getDate())
				+ " - " + getDoctor().getName() + " - " + this.getReason());
	}
}
